/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ctrl;

import carte.CarteStarWars;
import java.util.ArrayList;
import perso.DarkVador;
import perso.ImperialSoldier;
import perso.Personnage;
import perso.RebelPilot;
import perso.Yoda;

/**
 *
 * @author Benoît
 */
public class FactoryStarWars extends AbstractFactory {

    private CarteStarWars carte;
    public staticObj planets[][];
    private Personnage[][] posPerso;

    public CarteStarWars createMap(int width, int height) {
        posPerso = new Personnage[width][height];

        planets = new staticObj[width][height];
        addPlanetsInMap();

        carte = CarteStarWars.getInstance(posPerso, planets);
        return carte;
    }

    public ArrayList<Personnage> populateWorld(int nbrRebelPilot, int nbrImperialSoldiers) {
        ArrayList<Personnage> listPerso = new ArrayList<>();
        Yoda yoda = Yoda.getInstance(nbrPerso++, carte);
        DarkVador darkvador = DarkVador.getInstance(nbrPerso++, carte);

        addPersoInMap(yoda, 1, 1, true);
        listPerso.add(yoda);
        
        addPersoInMap(darkvador, planets.length-2, planets[planets.length-2].length-2, true);
        listPerso.add(darkvador);

        for (int a = 0; a < nbrRebelPilot; a++) {
            RebelPilot rebel = new RebelPilot(nbrPerso++, carte);
            addPersoInMap(rebel, 1, 1, false);
            listPerso.add(rebel);
        }
        for (int a = 0; a < nbrImperialSoldiers; a++) {

            ImperialSoldier soldier = new ImperialSoldier(nbrPerso++, carte);
            addPersoInMap(soldier, planets.length-2, planets[planets.length-2].length-2, false);
            listPerso.add(soldier);
        }
        return listPerso;
    }

    private void addPersoInMap(Personnage perso, int xPlanet, int yPlanet, boolean b) {
            if(b){
                posPerso[xPlanet][yPlanet] = perso;
                perso.px = xPlanet;
                perso.py = yPlanet;
                return;
            }
            int baseX = xPlanet + 1;
            int baseY = yPlanet + 1;
            for(int i = 0; i <= 2; i++){
               for(int j = 0; j <= 2 ; j++){
                   if(posPerso[baseX - i][baseY - j] == null){
                       posPerso[baseX - i][baseY -j] = perso;
                       perso.px = baseX - i;
                       perso.py = baseY - j;
                       return;
                   }
               }
            }
    }

    private void addPlanetsInMap() {
        int dagobahX = 1;
        int dagobahY = 1;
        int darkStarX = planets.length-2;
        int darkStarY = planets[planets.length-2].length-2;

        planets[dagobahX][dagobahY] = staticObj.DAGOBAH;
        planets[darkStarX][darkStarY] = staticObj.DARKSTAR;
    }
}
